/**
 * @class refrigeratory.pn_store
 * @extends Ext.Panel
 *
 * refrigeratory.pn_store description
 *
 * @author    Charlie
 * @copyright (c) 2011, refrigeratory
 * @date      July 2011
 * @version   0.1
 * @revision  0.1
 *
 */
 
// create namespace
Ext.ns('refrigeratory');

/**
 * Creates new refrigeratory.pn_store
 * @constructor
 * @param {Object} config A config object
 */
refrigeratory.pn_store = Ext.extend(Ext.Panel, {
	
    url: refrigeratory.REQUEST_URL

    ,initComponent:function() {
 
        // hard coded config (it cannot be changed while instantiating)
        // {{{
        var config = {        	
        	title: this.titleText
        	,id : 'pn_store'
			,closable: true
    		,autoScroll: true
    		,style:"height:100%;"
    		,bodyStyle:"height:100%;"
        }; // eo config object
 
        // apply config
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        // }}}
 
        // call parent
        refrigeratory.pn_store.superclass.initComponent.apply(this, arguments);
 
        // parent call post-processing, e.g. install event handlers
 
    } // eo function initComponent

    ,onRender:function() {
     
        // parent call pre-processing
 
        // call parent
        refrigeratory.pn_store.superclass.onRender.apply(this, arguments);
 
        // parent call post-processing, e.g. install event handlers on rendered components
        this.add(this.createGp(),this.createBp());
        this.doLayout();
 
    } // eo function onRender
    
    // 需本地化的文本
    ,titleText: '仓库管理'
    ,addText: "增加"
    ,deleteText: "删除"
    ,saveText: '保存'

    // icon css class
    ,iconDelete: 'refrigeratory-icon-delete'
    ,iconAdd: 'refrigeratory-icon-add'
    ,iconSave: 'refrigeratory-icon-save'
    
    // private
    ,createGp: function(){
    	var store = new Ext.data.JsonStore({
    		url: this.url
    		,baseParams: {
    			action:'storage',
    			operation: 'info_storage'
    		}
    		,root: 'data'
    		,fields:['id_storage','name','location']
    		,autoLoad: false
    	});
    	store.on('load', this.onJstGpLoad, this);
    	store.on('beforeload', this.onJstGpBeforeLoad, this);
    	var renderGroup = function(value,metadata,record){  
    		var index = this.jstGroup.find("gid", value);
    		if (index!=-1)
    			return this.jstGroup.getAt(index).data["name"];
    		else
    			return value;
    	};
		
    	var cm = new Ext.grid.ColumnModel([
    		new Ext.grid.RowNumberer(),
    		{header: "冷库名",		dataIndex:'name',  sortable: true, width:160}
			,{header: "地址",		dataIndex:'location', width:360}
    	]);
		
		sm = new Ext.grid.RowSelectionModel({
            singleSelect:true
        });
		
    	var o = this.gp = new Ext.grid.GridPanel({
    		//title: this.titleText
			baseCls: 'x-plain'
    		,ds: store
    		,cm: cm
			,sm:sm
			,stripeRows: true
    		,tbar: this.createGpTbar()
    		,height: 250
    		,border: false
    		,viewConfig: {
    			forceFit: false
    		}
    	});
		o.getSelectionModel().on('rowselect', function(sm, rowIdx, r) {
			var bp = Ext.getCmp('pn_bottom');
			bp.store.reload({
				params : {
					action:'equipment'
					,operation: 'info_equipment'
					,id_storage : rowIdx+1
				}
			});
		});
		
		this.reloadGp();
    	return o;
    }
	
	// private
	,createBp : function() {
		var store = new Ext.data.JsonStore({
    		url: this.url
    		,baseParams: {
    			action:'equipment'
    			,operation: 'info_equipment'
				,id_storage : 1
    		}
    		,root: 'data'
    		,fields:['id_equipment','identifier','time','temp']
    		,autoLoad: true
    	});
		
		this.createRowActions();
			
		var cm = new Ext.grid.ColumnModel([
    		new Ext.grid.RowNumberer(),
    		{header: "设备",dataIndex:'identifier',  sortable: true, width:160}
			,{header: "温度",dataIndex:'temp', width:360}
			,{header: "更新时间",dataIndex:'time', width:360}
			//,{header: "详细", dataIndex:'id_equipment', width:100, renderer:detials.createDelegate(this)}
    	]);	
		var o = this.bp = new Ext.grid.GridPanel({
			id:'pn_bottom'
    		,ds: store
    		,cm: cm
    		,autoHeight: true
    		,border: false
			,tbar: this.createBpTbar()
    		,viewConfig: {
    			forceFit: false
    		}
    	});
		store.on('load',function(){
			o.getSelectionModel().selectFirstRow();
		})
		return o;
	}

    // private
    ,createGpTbar: function(){
    	var tb = [];
    	this.btnRefresh = new Ext.Toolbar.Button({
    		text: "刷新"
    		,handler: this.onBtnRefreshClick.createDelegate(this)
    	});
    	tb.push(this.btnRefresh);
    	return tb;
    }
	
	,createBpTbar: function(){
		var tb = [];		
		this.btnDetials = new Ext.Toolbar.Button({
    		text: "详细"
    		,handler: this.onBtnDetialsClick.createDelegate(this)
    	});	
		tb.push(this.btnDetials);
		return tb;
	}
	
	,createRowActions:function(){
		var o  = this.rowActions = new Ext.ux.grid.RowActions({
            header: "操作"
            ,width: 80
            ,autoWidth: false
            ,actions:[{
                iconIndex: 'icon'
                ,qtipIndex: 'qtip'
            }]
        });
        o.on('action', this.onRowActionsClick, this);
        return this.rowActions;
	}
  
    // private
    ,onJstGpBeforeLoad: function(){
    	this.getEl().mask("数据载入中...");
    }
    
    // private
    ,onJstGpLoad: function(store, records, options){
    	this.getEl().unmask();
    } // private

    // private
    ,onBtnRefreshClick: function(){
    	this.reloadGp();
    }
	
	,onBtnDetialsClick:function(){
		var record = this.bp.getSelectionModel().getSelected();
		var d = record.data;
		data = this.createTempData(d.id_equipment);
		//this.showDetial(id_equipment);
	}
    
    // private
    ,onJstGroupLoad: function(store, records, options){
    	this.gp.getStore().load();
    }  
    // private
    ,reloadGp: function(){
        this.gp.store.reload();
    }
	// private
	,showDetial : function(data){
		var series = [{data: data}];
		var win = new Ext.Window({
			title: 'Basic',
			//renderTo:parent.Ext.getBody(),
			modal:true,
			width: 600,
			height: 480,
			layout: 'fit',
			items: [{
			  xtype: 'flot',
			  cls: 'x-panel-body',
			  xaxis: { mode: "time" },
			  grid: { markings: Ext.ux.Flot.grid.weekendMarkings },
			  series: series
			}]
		});
		win.show();	
	}
	//private
	,createTempData:function(id_equip){
		var d1 = [];
		var requestConfig ={  
			url:'get.php'
			,params:{
    			action:'chart'
    			,operation: 'info_chart_equip'
				,id_equip : id_equip
				,get_temp : true
				,get_humidity : true 
    		}
			,scope:this
			,callback:function(options,success,response){  
				jsonData = Ext.util.JSON.decode(response.responseText);
				for(var i=0;i<jsonData.count;i++){  
					d1.push([jsonData.data[i].time,jsonData.data[i].temp]);  
				}
				this.showDetial(d1);				
			}   
		}  
		Ext.Ajax.request(requestConfig); 		
	}
	
	// private
    ,onRowActionsClick: function(grid, record, action, row, col){ 	
    	if (action == this.detials){
			var iphone = this.gp.store.getAt(row).get('iphone');
			var iid = this.gp.store.getAt(row).get('iid');
			this.requestDelete(iphone,iid);
		}         
    }
}); // eo extend
 
// register xtype
Ext.reg('pn_store', refrigeratory.pn_store); 
// eof